/**
 * 
 */
package com.scpii.universal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ActiveUserDto;
import com.scpii.universal.hibernate.dao.ActiveUserDao;
import com.scpii.universal.hibernate.dao.ActiveUserDtoDao;
import com.scpii.universal.hibernate.entity.ActiveUserTab;
import com.scpii.universal.service.ActiveUserService;

/**
 * 
 * genericWeb
 * 
 * @author weny 2012-7-5下午05:33:09
 */
@Service
public class ActiveUserServiceImpl extends BaseServiceImpl<ActiveUserTab, Integer> implements
		ActiveUserService {
	@Resource
	private ActiveUserDao activeUserDao;
	@Resource
	private ActiveUserDtoDao activeUserDtoDao;

	@Resource
	public void setBaseDao(ActiveUserDao activeUserDao) {
		super.setBaseDao(activeUserDao);
	}

	private Log log = LogFactory.getLog(this.getClass());

	/**
	 * 获取参与一个活动人员的详细情况
	 */
	@Override
	public List<ActiveUserDto> getActiveLsit(ActiveUserDto condition, Integer pageNo, Integer pageSize) {

		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.ActiveUserDto(t1.datJoinActiveDatetime,");
		hql.append(" t2.varContentName,");
		hql.append(" t3.varUserPhonenum,t3.varUserLoginName )");
		hql.append(" from ActiveUserTab t1 , ");
		hql.append("  ContentTab t2 , ");
		hql.append("  ClientUserTab t3 ");
		hql.append(" where t1.intContentId= t2.intContentId ");
		hql.append(" and  t1.intUserId= t3.intUserId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign");
		hql.append(" and  t1.intContentId=:intContentId");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intContentId", condition.getIntContentId());
		List<ActiveUserDto> activeList = new ArrayList<ActiveUserDto>();

		activeList = activeUserDtoDao.findByHql(hql.toString(), pageNo, pageSize, argsMap);

		return activeList;
	}

	/**
	 * 获取参与一个活动的总人数
	 */
	@Override
	public Integer getActivelistCount(Integer intContentId) {
		StringBuffer hql = new StringBuffer("SELECT count(*) ");
		hql.append(" from ActiveUserTab t1 , ");
		hql.append("  ContentTab t2 , ");
		hql.append("  ClientUserTab t3 ");
		hql.append(" where t1.intContentId= t2.intContentId ");
		hql.append("  and t1.intContentId=:intContentId ");
		hql.append(" and  t1.intUserId= t3.intUserId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign");
	

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intContentId", intContentId);
		List activeList = new ArrayList();

		activeList = activeUserDtoDao.hqlGetList(hql.toString(), argsMap);
		if (activeList != null && activeList.size() == 1) {
			return Integer.parseInt(activeList.get(0).toString());
		} else {
			return 0;
		}

	}

	@Override
	public List<ActiveUserDto> getAllActiveList(ActiveUserDto condition, Integer pageNo, Integer pageSize, int appId) {
		StringBuffer hql = new StringBuffer(
				"SELECT distinct new com.scpii.universal.dto.ActiveUserDto(t1.intContentId,t1.datJoinActiveDatetime,");
		hql.append(" t2.varContentName,");
		hql.append(" t1.intActiveUser) ");
		hql.append(" from ActiveUserTab t1 , ");
		hql.append("  ContentTab t2  ");		
		hql.append(" where t1.intContentId= t2.intContentId ");		
		hql.append(" and t1.intDeleteSign=:intDeleteSign");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intAppId=:intAppId ");
		hql.append(" order by t1.intContentId ");	

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intAppId", appId);

		List<ActiveUserDto> activeList = new ArrayList<ActiveUserDto>();

		activeList = activeUserDtoDao.hqlGetList(hql.toString(),  argsMap);

		return activeList;
	}

	@Override
	public List<ActiveUserDto> getAllActiveCount(int appId) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.ActiveUserDto(t1.intContentId) ");
		hql.append(" from ActiveUserTab t1");
		hql.append(",ContentTab t2  ");
		hql.append(" where t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intAppId=:intAppId ");
		hql.append("   group by t1.intContentId  ");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intAppId", appId);

		List<ActiveUserDto> activeList = new ArrayList<ActiveUserDto>();

		activeList = activeUserDtoDao.hqlGetList(hql.toString(), argsMap);
		
	    return activeList;
		

	}

}
